import { ProcessService } from './../../../@core/services/process/process.service';
import { Component, Input } from '@angular/core';
import { TableWidthConfig } from 'ng-devui';
import { JobOrderService } from 'src/app/@core/services/jobOrder/job-order.service';
import { UserService } from 'src/app/@core/services/user/user.service';
@Component({
  selector: 'app-assign-user',
  templateUrl: './assign-user.component.html',
  styleUrls: ['./assign-user.component.scss']
})
export class AssignUserComponent {
  tabActiveId: string | number = '工序人员';
  @Input() data: any;
  isSelected = false;
  displayTableData: any[] = [];
  tableheight = '230px';
  selectionheight = '280px'
  selectData: any;
  searchName: ''
  pager = {
    total: 20,
    pageIndex: 1,
    pageSize: 20
  };
  pageSizeOptions = [10, 20, 30, 50, 100];
  dataTableOptions = {
    columns: [
      {
        field: 'code',
        header: '员工编码',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '员工名称',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '工号',
        fieldType: 'text',
      },

      {
        field: 'name',
        header: '部门',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '操作',
        fieldType: 'text',
      },
    ]
  };
  tableWidthConfig: TableWidthConfig[] = [
    {
      field: 'radio',
      width: '100px'
    },
    {
      field: 'code',
      width: '100px'
    },
    {
      field: 'code',
      width: '100px'
    },
    {
      field: 'code',
      width: '100px'
    },

    {
      field: 'name',
      width: '100px'
    }
  ]
  selectionData = {
    columns: [
      {
        field: 'name',
        header: '操作',
        fieldType: 'text',
      },
      {
        field: 'code',
        header: '员工编码',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '员工名称',
        fieldType: 'text',
      },
    ]
  }
  selectionWidthCon: TableWidthConfig[] = [
    {
      field: 'radio',
      width: '100px'
    },
    {
      field: 'code',
      width: '100px'
    },
    {
      field: 'code',
      width: '100px'
    },
  ]
  selection = []
  assginData: any = {
    jobOrderList: [
      {
        jobOrderProcessID: 0
      }
    ],
    employeeCodes: [
    ]
  };
  revoke = [{
    jobOrderProcessID: 0
  }]
  userName = []
  assignedItems: any[] = [];
  tableData: any=[]
  constructor(private UserService: UserService, private ProcessService: ProcessService,private JobOrderService:JobOrderService ) { }
  ngOnInit(): void {
    const newArray = this.data.data.map((item: any) => ({
      jobOrderProcessID: item.jobOrderProcessId,
    }))
    this.revoke = this.data.data.map((item: any) => ({
      jobOrderProcessID: item.jobOrderProcessId,
    }))
    this.assginData.jobOrderList = newArray
      this.selection = this.data.data.map((item: any) => item.users).flat().filter((user: any, index: number, self: any[]) =>
        index === self.findIndex((u: any) => u.employeeCode === user.employeeCode)
      );
      this.assignedItems = this.data.data.map((item: any) => item.users).flat();
      this.assginData.employeeCodes = this.data.data.map((item: any) => item.users).flat().map((item: any) => item.employeeCode);
      this.userName = this.data.data.map((item: any) => item.users).flat().map((item: any) => item.name)

    this.UserService.GetAllEmploy(this.pager.pageIndex, this.pager.pageSize).subscribe(
      (data: any) => {
        this.displayTableData = data.items;
        this.pager.total = data.totalCount;
      }
    )
    this.data.data[0].processId
    this.ProcessService.GetProcessById(this.data.data[0].processId).subscribe((res: any) => {
      this.tableData = res.processUsers
      if (this.data.title == '撤回') {
        this.tableData = this.tableData.map((item: any) => {
          item.users = item.users.map((item: any) => {
            item.employeeCode = item.code
            return item
          })
          return item
        })
      }
    })
  }
  pageIndexChange($event: any) {
    this.pager.pageIndex = $event;
    this.UserService.GetAllEmploy(this.pager.pageIndex, this.pager.pageSize).subscribe((data: any) => {
      this.displayTableData = data.items;
      this.pager.total = data.totalCount;
    })
  }//切换页码
  pageSizeChange($event: any) {
    this.UserService.GetAllEmploy(this.pager.pageIndex, this.pager.pageSize).subscribe((data: any) => {
      this.displayTableData = data.items;
      this.pager.total = data.totalCount;
    })
  }//改变每页显示数量
  search() {
    this.UserService.GetUserByDepartmentId(null, null, this.searchName, this.pager.pageIndex, this.pager.pageSize).subscribe((res: any) => {
      this.displayTableData = res.items
      this.pager.total = res.totalCount
    })
  }
  reset() {
    this.searchName = ''
    this.UserService.GetAllEmploy(this.pager.pageIndex, this.pager.pageSize).subscribe((res: any) => {
      this.displayTableData = res.items
      this.pager.total = res.totalCount
    })
  }
  assign(rowItem: any) {
    this.assginData.employeeCodes = []
    this.assginData.employeeCodes.push(rowItem.employeeCode)
    this.JobOrderService.AssignProcessesToUser(this.assginData).subscribe((data:any)=>{
      this.showToast('success','提示','分配成功')
      this.assignedItems.push(rowItem);
      this.userName.push(rowItem.name)
      this.selection.push(rowItem)
      this.assginData.employeeCodes = []
    },(error:any)=>{
      if(error.status==404){
        this.showToast('error','提示','服务器连接失败')
      }else{
        this.showToast('error','提示',error.error)
      }
    }
  )
  }
  isAssigned(rowItem: any): boolean {
    return this.assignedItems.some(item => item.employeeCode === rowItem.employeeCode);
  }
  delete(rowItem: any) {
    this.revoke.map((item:any)=>{
      this.JobOrderService.RevokeAssignUserFromProcess(item.jobOrderProcessID,[rowItem.employeeCode]).subscribe((data:any)=>{
        this.showToast('success','提示','撤回成功')
        this.selection = this.selection.filter(item => item !== rowItem)
        this.assginData.employeeCodes = this.assginData.employeeCodes.filter((item: any) => item !== rowItem.employeeCode)
        this.userName = this.userName.filter((item: any) => item !== rowItem.name)
        this.assignedItems = this.assignedItems.filter(item => item !== rowItem);
      },(error:any)=>{
        this.showToast('error','提示',error.error)
      }
    )
    })
  }
  msgs: Array<Object> = [];
  showToast(type: any,summary:any,content:any) {
    switch (type) {
    default:
      this.msgs = [{ severity: type, summary: summary, content: content }];
    }
  }//提示框
}
